# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=exiv2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.27.7
pkgrel=2
pkgdesc="Exif/IPTC/Xmp C++ metadata library and tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64')
url='https://exiv2.org/'
msys2_references=(
  "cpe: cpe:/a:andreas_huggel:exiv2"
  "cpe: cpe:/a:exiv2:exiv2"
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/Exiv2/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('551b1266e3aabd321f6d555dccd776128ee449d5039feafee927a1f33f7a9753')

build() {
  #shared
  mkdir -p "${srcdir}"/build-shared-${MSYSTEM} && cd "${srcdir}"/build-shared-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DEXIV2_ENABLE_VIDEO=ON \
      -DEXIV2_ENABLE_NLS=ON \
      -DEXIV2_ENABLE_WIN_UNICODE=ON \
      -DEXIV2_BUILD_SAMPLES=OFF \
      -DEXIV2_ENABLE_WEBREADY=ON \
      -DEXIV2_ENABLE_CURL=ON \
      -DEXIV2_ENABLE_BMFF=ON \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  
  #static
  mkdir -p "${srcdir}"/build-static-${MSYSTEM} && cd "${srcdir}"/build-static-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DCMAKE_CXX_FLAGS="-DCURL_STATICLIB" \
      -DCMAKE_C_FLAGS="-DCURL_STATICLIB" \
      -DBUILD_SHARED_LIBS=OFF \
      -DEXIV2_ENABLE_VIDEO=ON \
      -DEXIV2_ENABLE_NLS=ON \
      -DEXIV2_ENABLE_WIN_UNICODE=ON \
      -DEXIV2_BUILD_SAMPLES=OFF \
      -DEXIV2_ENABLE_WEBREADY=ON \
      -DEXIV2_ENABLE_CURL=ON \
      -DEXIV2_ENABLE_BMFF=ON \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-static-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  cd "${srcdir}"/build-shared-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
